package org.xiaojl.common.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.xiaojl.common.Constants;
import org.xiaojl.common.base.BaseController;
import org.xiaojl.config.shiro.captcha.DreamCaptcha;
import org.xiaojl.utils.DateUtil;
import org.xiaojl.utils.FileUpload;
import org.xiaojl.utils.ToolUtil;

@Controller
public class CommonsController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(CommonsController.class);
	@Autowired
    private DreamCaptcha dreamCaptcha;
	
	 /**
     * 图形验证码
     */
    @RequestMapping("captcha.jpg")
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        dreamCaptcha.generate(request, response);
    }
    
    /**
     * webuploader文件上传功能
     */
    @RequestMapping("pictures/save")
    @ResponseBody
    public Object webUploaderSave(@RequestParam(required=false) MultipartFile file) {
    	String basePath = getContextPath();
    	String fileDateName = DateUtil.getDays();
    	String imgPath = Constants.IMG_SAVE_PATH +fileDateName +File.separator ;//文件上传相对路径，相对与项目的webroot
    	String filePath = basePath + imgPath;//文件上传绝对路径
    	String fileName = "";
    	if (null != file && !file.isEmpty()) {
			fileName = FileUpload.fileUp(file, filePath, ToolUtil.getUid());//执行上传
		}else{
			log.info("上传失败：file is null");
		}
    	
    	log.debug("文件上传成功：绝对路径="+filePath +File.separator+fileName);
    	log.debug("文件上传成功：相对路径="+imgPath +File.separator+fileName);
    	
    	return operateSuccess("文件上传成功",imgPath +File.separator+fileName) ;
    }
    
    /**
     * 查看 图标
     */
    @RequestMapping("foundIcons")
    public String foundIcons() {
    	log.debug("进入查看系统图标页面...");
    	return "foundIcons";
    }
}
